package com.xiyou.luckdraw.web.config.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import lombok.Data;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AbstractMongoConfig
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-21 17:12
 */
@Data
public abstract class AbstractMongoConfig {
    // 变量名跟配置的参数对应
    private String host;
    private int port;
    private String username;
    private String password;
    private String database;


    public MongoDbFactory mongoDbFactory() throws Exception {
        // 无认证的初始化方法
        // return new SimpleMongoDbFactory(new MongoClient(host, port), database);

        // 有认证的初始化方法
        ServerAddress serverAddress = new ServerAddress(host, port);
        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        mongoCredentialList.add(MongoCredential.createCredential(username, database, password.toCharArray()));
        return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), database);
    }


    /*
     * Factory method to create the MongoTemplate
     */
    abstract public MongoTemplate getMongoTemplate() throws Exception;
}
